package leetcode;

import java.util.Arrays;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-26 10:47
 **/
public class LeetCode5964 {

    public int[] executeInstructions(int n, int[] startPos, String s) {

        int m = s.length();
        int[] ans = new int[m];
        for (int i = m - 1; i >= 0; --i) {
            int j = i;
            int[] temp = Arrays.copyOf(startPos, startPos.length);
            while (j <= m - 1) {
                char c = s.charAt(j);
                boolean flag = false;
                switch (c) {
                    case 'U' : {
                        if (temp[0] - 1 >= 0) {
                            ++ans[i];
                            temp[0] -= 1;
                            flag = true;
                        }
                        break;
                    }

                    case 'D' : {
                        if (temp[0] + 1 <= n - 1) {
                            ++ans[i];
                            temp[0] += 1;
                            flag = true;
                        }
                        break;
                    }

                    case 'L' : {
                        if (temp[1] - 1 >= 0) {
                            ++ans[i];
                            temp[1] -= 1;
                            flag = true;
                        }
                        break;
                    }

                    default : {
                        if (temp[1] + 1 <= n - 1) {
                            ++ans[i];
                            temp[1] += 1;
                            flag = true;
                        }
                    }
                }

                if (flag) {
                    ++j;
                } else {
                    break;
                }
            }

        }
        return ans;
    }

    public static void main(String[] args) {
        LeetCode5964 code = new LeetCode5964();
        System.out.println(Arrays.toString(code.executeInstructions(3, new int[]{0, 1}, "RRDDLU")));
    }
}
